﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RipPoll.Library;


namespace RipPoll.Web
{
    public partial class CreatePoll : RippollBasePage
    {
        const string sessionvar_LoggedUser = "LoggedUser";
        const string controlRootName = "ctl00$rippollMainBody$";
        const string controlRootId = "rippollMainBody_";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Master != null)
                ((RippollMaster)this.Master).PageTitle = "Create Poll";

            if (Session[sessionvar_LoggedUser] == null)
            {
                UserPE loggeduser = new UserServices.UserServicesClient().GetUserById(1);
                Session[sessionvar_LoggedUser] = loggeduser;
            }
            if (!IsPostBack)
            {
                PollServices.PollServicesClient pbo = new PollServices.PollServicesClient();
                List<PollCategoryPE> categories = pbo.GetPollcategories();
                rdoCategoryList.CssClass = "smallTextWhite";
                if (categories != null)
                {
                    for (int i = 0; i < categories.Count; i++)
                    {
                        PollCategoryPE pcpe = categories[i];
                        rdoCategoryList.Items.Add(new ListItem(pcpe.Category, pcpe.Id.ToString()));
                    }
                }
            }

            //if (IsPostBack)
            //{   
            //    rdoCategoryList.Items.FindByValue(categoryTypeId.Value).Selected = true;
            //}

            
        }

        protected void btncreatePoll_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtPollName.Text))
            {
                UpdateStatus("Poll Nam cannot be null.");
                return;
            }

            if (string.IsNullOrEmpty(categoryTypeId.Value))
            {
                 UpdateStatus("Select poll category.");
                return;
            }

            if (string.IsNullOrEmpty(choiceTypeId.Value))
            {
                 UpdateStatus("Select Choice Id.");
                return;
            }



            PollPE newPoll = new PollPE();
            newPoll.Name = txtPollName.Text;
            newPoll.Choices = new List<ChoicePE>();
            newPoll.Category = new PollCategoryPE() { Id = int.Parse(rdoCategoryList.SelectedItem.Value), Category = rdoCategoryList.SelectedItem.Text };

            if (choiceTypeId.Value == "1")
            {
                newPoll.ChoiceGroupId = 1;
            }
            else
            {
                ChoicePE newChoice = null;
                for (int i = 1; i <= int.Parse(totalchoicecount.Value); i++)
                {
                    newChoice = new ChoicePE();
                    string choiceText = Request.Form[controlRootName + "txtChoice" + i.ToString()];
                    if(string.IsNullOrEmpty(choiceText))
                    {
                         UpdateStatus("Enter value for choice #" + i.ToString());
                        return;
                    }
                    else
                        newChoice.Choice = choiceText;
                    newPoll.Choices.Add(newChoice);
                }
                CommonServices.CommonServicesClient cbo = new CommonServices.CommonServicesClient();
                cbo.InsertChoices(newPoll.Choices);

                newPoll.ChoiceGroupId = newPoll.Choices[0].ChoiceGroupId;
            }
            PollServices.PollServicesClient pbo = new PollServices.PollServicesClient();
            pbo.AddPoll(newPoll, (UserPE)Session[sessionvar_LoggedUser]);

            UpdateStatus("Poll got Created......");
        }
    }
}